<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryCode">
              <a-input
                :disabled="formDisabled"
                v-model="model.categoryCode"
                placeholder="请输入编号"
                disabled
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date
                :disabled="formDisabled"
                placeholder="请选择日期"
                v-model="model.createTime"
                :show-time="true"
                date-format="YYYY-MM-DD HH:mm"
                style="width: 100%"
                disabled
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="经办人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposer">
              <select-person-input :disabled="formDisabled" v-model="model.proposer" disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="经办部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="proposerDeptId">
              <j-select-depart :disabled="formDisabled" v-model="model.proposerDeptId" multi disabled />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryName">
              <a-input :disabled="formDisabled" v-model="model.categoryName" placeholder="请输入名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="审批人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="approvalUser">
              <select-user-by-dep
                :disabled="formDisabled"
                :multi="false"
                v-model="model.approvalUser"
                placeholder="请选择审批人"
              ></select-user-by-dep>
            </a-form-model-item>
          </a-col>
          <!--          <a-col :span="24">-->
          <!--            <a-form-model-item label="编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryCode">-->
          <!--              <a-input :disabled=formDisabled v-model="model.categoryCode" placeholder="请输入编码"></a-input>-->
          <!--            </a-form-model-item>-->
          <!--          </a-col>-->
          <a-col :span="12" v-if="isShowParent">
            <a-form-model-item label="上级分类编码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
              <a-input :disabled="formDisabled" v-model="model.parentCode"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="isShowParent">
            <a-form-model-item label="上级分类名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="parentId">
              <a-input :disabled="formDisabled" v-model="model.parentName"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="分类等级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="level">
              <j-dict-select-tag
                :disabled="disabledLevel"
                type="list"
                v-model="model.level"
                dictCode="business_category_level"
                placeholder="请选择分类等级"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12" v-if="model.level == 1">
          <a-form-model-item label="负责人岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaderPostCode">
            <j-search-select
              :disabled="formDisabled"
              v-model="model.leaderPostCode"
              dict="sys_position,name,code,del_flag = 0 and audit_status=2"
              placeholder="请选择岗位"
            />
          </a-form-model-item>
        </a-col>
          <a-col :span="12">
            <a-form-model-item label="标准工时(人/天)" prop="standardHours">
              <a-input-number
                style="width: 100%"
                step="0.1"
                :precision=1
                :disabled="formDisabled"
                v-model="model.standardHours"
                placeholder="请输入标准工时"
              ></a-input-number>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea
                :disabled="formDisabled"
                :max-length="500"
                v-model="model.remark"
                rows="4"
                placeholder="请输入备注"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">
              <j-dict-select-tag
                :disabled="formDisabled"
                type="radio"
                v-model="model.status"
                dictCode="ENABLE_OR_DISABLE"
                placeholder="请选择状态"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
import { getAction, httpAction } from '@/api/manage'
import SelectTeamInputModal from '@views/scy/components/modules/SelectCustomerInputModal'
import { mapGetters } from 'vuex'
import moment from 'moment'
import SelectPersonInput from '@/views/scy/components/SelectPersonInput'
import JSearchSelect from '@/views/scy/components/JSearchSelect.vue'

export default {
  name: 'ErpFilesBusinessCategoryForm',
  components: {
    SelectUserByDep,
    SelectTeamInputModal,
    SelectPersonInput,
    JSearchSelect
  },
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    var validateLevel = async (rule, value, callback) => {
      if (value && value == '1') {
        // this.$refs.form.clearValidate('technologyLeader');
        this.$set(this.validatorRules, 'technologyLeader', [])
      } else {
        this.$set(this.validatorRules, 'technologyLeader', [{ required: true, message: '请输入技术负责人!' }])
      }
    }
    return {
      isShowParent: false,
      disabledLevel: false,
      model: {
        status: 'enabled',
      },
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        categoryName: [{ required: true, message: '请输入名称!' }],
        // technologyLeader: [
        //   {required: true ,message: '请输入技术负责人!' },
        // ],
        parentId: [{ required: false, message: '请输入上级分类编码!' }],
        level: [{ required: true, message: '请输入分类等级!' }],
        status: [{ required: true, message: '请输入状态!' }],
      },
      url: {
        add: '/api/erp/files/category/filesBusinessCategory/add',
        edit: '/api/erp/files/category/filesBusinessCategory/edit',
        queryById: '/api/erp/files/category/filesBusinessCategory/queryById',
        generatedFormNumber: '/api/erp/base/generatedFormNumber',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    ...mapGetters(['userInfo']),
    generatedFormNumber() {
      getAction(this.url.generatedFormNumber, { prefix: 'YWFL', subSystemCode: '05' }).then((res) => {
        if (res.success) {
          this.$set(this.model, 'categoryCode', res.result)
        }
      })
    },
    add(record) {
      console.log('record', record)
      this.$refs.form.resetFields()
      this.generatedFormNumber()
      if (record) {
        this.isShowParent = true
        this.model.parentId = record.id
        this.model.level = parseInt(record.level) + 1
        this.model.level = this.model.level.toString()
        this.disabledLevel = false
        this.validatorRules.parentId.required = true
        this.model.parentCode = record.categoryCode
        this.model.parentName = record.categoryName
      } else {
        this.isShowParent = false
        this.model.level = '1'
        this.disabledLevel = true
        this.validatorRules.parentId.required = false
      }
      this.modelDefault = Object.assign(this.modelDefault, this.model)
      console.log('this.model', this.model)
      this.edit(this.modelDefault)
      this.userName = this.userInfo().empName //申请人名称
      this.model.proposer = this.userInfo().empId //申请人
      this.model.proposerDeptId = this.userInfo().deptId //申请部门
      this.model.createTime = moment().format('YYYY-MM-DD HH:mm')
    },
    edit(record) {
      this.$refs.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warn(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>
